package cn.maihe.elg.operation.centers.hymake.handler.service;

import cn.maihe.elg.operation.centers.hymake.config.HyMakeInfoConfig;
import cn.maihe.elg.operation.centers.hymake.dto.handler.HyMakeCheckGuaranteeReqDTO;
import cn.maihe.elg.operation.centers.hymake.dto.handler.HyMakeCheckGuaranteeRespDTO;
import cn.maihe.elg.operation.centers.hymake.dto.handler.vo.HyMakeBondCheckDTO;
import cn.maihe.elg.operation.centers.hymake.dto.handler.vo.HyMakeBondCheckItemDTO;
import cn.maihe.elg.operation.centers.hymake.enums.HyMakeCheckResultEnum;
import cn.maihe.elg.operation.centers.hymake.handler.HyMakeCenterTradeService;
import cn.maihe.elg.operation.centers.supports.CenterTradeContext;
import cn.maihe.elg.operation.centers.supports.annotation.CenterTradeService;
import cn.maihe.elg.operation.model.enums.AcceptOrderStatus;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.model.enums.CenterProviderEnum;
import cn.maihe.elg.operation.model.enums.TradeType;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.service.order.ApplyOrderInfoService;
import cn.maihe.elg.operation.service.order.RefundOrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 保函验真申请
 * @Author caixh
 * @Date 2022/3/30.
 */
@CenterTradeService(
        provider = CenterProviderEnum.HYMAKE,
        centerNo = CenterNoEnum.NINGDE,
        tradeType = TradeType.checkTrue,
        reqClass = HyMakeCheckGuaranteeReqDTO.class,
        respClass = HyMakeCheckGuaranteeRespDTO.class
)
@Slf4j
public class HyMakeCheckGuaranteeService extends HyMakeCenterTradeService<HyMakeCheckGuaranteeReqDTO, HyMakeCheckGuaranteeRespDTO> {

    @Resource
    ApplyOrderInfoService applyOrderInfoService;
    @Resource
    AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    RefundOrderInfoService refundOrderInfoService;

    public HyMakeCheckGuaranteeService(HyMakeInfoConfig centerInfosConfig) {
        super(centerInfosConfig);
    }


    /**
     * 具体业务处理
     *
     * @param context
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public HyMakeCheckGuaranteeRespDTO processService(CenterTradeContext<HyMakeCheckGuaranteeReqDTO, HyMakeCheckGuaranteeRespDTO> context) {

        HyMakeCheckGuaranteeReqDTO req = context.getReq();
        Long centerId = context.getCenterId();
        Long orgId = context.getOrgId();


        List<HyMakeBondCheckItemDTO> itemDTOS = req.getPolicyList().stream().map(item -> {

            String policyNo = item.getPolicyNo();
            AcceptOrderInfoDO orderInfo = acceptOrderInfoService.getByGuaranteeNo(centerId, orgId, policyNo);
            if(orderInfo == null){
                return HyMakeBondCheckItemDTO.builder()
                        .policyNo(policyNo)
                        .checkResult(HyMakeCheckResultEnum.FAILURE.getCheckResult())
                        .checkMessage("保函信息不存在")
                        .build();
            }
            if (orderInfo.getStatus() != AcceptOrderStatus.HAS_OUT_GUARANTEE.getStatus()&&orderInfo.getStatus() != AcceptOrderStatus.REVOKE_FAIL.getStatus()) {
                return HyMakeBondCheckItemDTO.builder()
                        .policyNo(policyNo)
                        .checkResult(HyMakeCheckResultEnum.FAILURE.getCheckResult())
                        .checkMessage("保函无效")
                        .build();
            }

            return HyMakeBondCheckItemDTO.builder()
                    .policyNo(policyNo)
                    .checkResult(HyMakeCheckResultEnum.SUCCESS.getCheckResult())
                    .checkMessage("保函有效")
                    .build();

        }).collect(Collectors.toList());
        HyMakeCheckGuaranteeRespDTO respDTO = HyMakeCheckGuaranteeRespDTO.success("验真完成",HyMakeCheckGuaranteeRespDTO.class);
        respDTO.setData(new HyMakeBondCheckDTO(itemDTOS));
        return respDTO;
    }
}
